<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/10/0010
 * Time: 14:51
 */

namespace app\sports_admin\model;


use think\Model;

class OperationLog extends Model
{
    protected $table = 'operation_log';

    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    protected $hidden = ['update_time'];

    public function getRequestIpAttr($value)
    {
        return long2ip($value);
    }

    /**
     * 获取管理员登录日志
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 检索的开始时间
     * @param $end 检索的结束时间
     */
    public static function operationList($page,$rows,$sort,$sortOrder,$searchName,$start,$end)
    {
        $result = self::where(function($query)use ($searchName,$start,$end){
            if($searchName){
                $query->where('username','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}", "{$end}"]);
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $total = self::where(function($query)use ($searchName,$start,$end){
            if($searchName){
                $query->where('username','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}", "{$end}"]);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$total]);
    }
}